/*
 * Copyright (c) 2009, Servoy-stuff
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 * http://www.fsf.org/licensing/licenses/lgpl.txt
 */
package net.stuff.servoy.plugins.whois;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

import com.servoy.j2db.scripting.IScriptObject;

/**
 * @author Servoy Stuff
 * http///www.servoy-stuff.net/
 */
public class WhoisPluginProvider implements IScriptObject {
	
	private String server = "whois.networksolutions.com";
	private int port = 43;
	private int timeout = 30 * 1000; // unit is milliseconds
	
	public String js_query(String domainName) {
		return js_query(domainName, this.server, this.port, this.timeout);
	}
	
	public String js_query(String domainName, String server) {
		return js_query(domainName, server, this.port, this.timeout);
	}
	
	public String js_query(String domainName, String server, int port) {
		return js_query(domainName, server, port, this.timeout);
	}
	
	public String js_query(String domainName, String server, int port, int timeout) {
		try
		{
			// create the socket
			Socket socket = new Socket(server, port);
			socket.setSoTimeout(timeout);
			
			// create a reader to get the response from the server
			BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			
			// create an output stream to send our query to the server
			DataOutputStream out = new DataOutputStream(socket.getOutputStream());
			// call the service with the domainName supplied (and terminate with carriage return)
			out.writeBytes(domainName +" \r\n");
			
			// read the response from the server
			String str1 = null;
			StringBuffer buffer = new StringBuffer();
			while ((str1 = in.readLine()) != null) {
				buffer.append(str1);
				buffer.append("\r\n");
			}
			
			// close our stream and reader
			out.close();
			in.close();
			// close the socket
			socket.close();
			
			// return the result as String
			return buffer.toString();
		  	
		} catch (IOException ioEx) {
			return ioEx.getLocalizedMessage();
		  
		} catch (Exception ex) {
			return ex.getLocalizedMessage();
		}
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.scripting.IScriptObject#getAllReturnedTypes()
	 */
	public Class[] getAllReturnedTypes() {
		return null;
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.scripting.IScriptObject#getParameterNames(java.lang.String)
	 */
	public String[] getParameterNames(String methodName) {
		return new String[] {"domainName", "[server]", "[port]", "[timeout] "};
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.scripting.IScriptObject#getSample(java.lang.String)
	 */
	public String getSample(String methodName) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("\t// you call the whois server by providing a domain name and get the info in return:\n");
		buffer.append("\tvar result = plugins.whois.query('servoy.com');\n");
		buffer.append("\t// alternatively you can provide an alternate server (default is networksolutions.com):\n");
		buffer.append("\t//var result = plugins.whois.query('servoy.com','whois.internic.net');\n");
		buffer.append("\t// you can also provide a port if not standard (43 by default):\n");
		buffer.append("\t//var result = plugins.whois.query('servoy.com','whois.internic.net',43);\n");
		buffer.append("\t// and you can also provide a timeout length (unit is milliseconds, default is 30 seconds):\n");
		buffer.append("\t//var result = plugins.whois.query('servoy.com','whois.internic.net',43);\n");
		return buffer.toString();
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.scripting.IScriptObject#getToolTip(java.lang.String)
	 */
	public String getToolTip(String methodName) {
		return "Calls a whois server to retrieve information about the domain name you provide";
	}

	/* (non-Javadoc)
	 * @see com.servoy.j2db.scripting.IScriptObject#isDeprecated(java.lang.String)
	 */
	public boolean isDeprecated(String methodName) {
		return false;
	}

	/**
	 * @return the server
	 */
	public String js_getServer() {
		return this.server;
	}

	/**
	 * @param server the server to set
	 */
	public void js_setServer(String server) {
		this.server = server;
	}

	/**
	 * @return the port
	 */
	public int js_getPort() {
		return this.port;
	}

	/**
	 * @param port the port to set
	 */
	public void js_setPort(int port) {
		this.port = port;
	}

	/**
	 * @return the timeout
	 */
	public int js_getTimeout() {
		return this.timeout;
	}

	/**
	 * @param timeout the timeout to set
	 */
	public void js_setTimeout(int timeout) {
		this.timeout = timeout;
	}

}
